<?php

namespace App\Console\Commands;

use App\Console\Commands\Common\CommonCommand;
use App\Models\User\User;
use Carbon\Carbon;
use EasyWeChat\Factory;

class RemindUserPay extends CommonCommand {

    protected $signature = 'remindpay';

    public function handle() {

        $days = [3,7];
        $config = config('wechat.default');
        $templateId = 'sYeshmsfakbaeSS2nHfrdOXk3Saeay_4UZG1AH3yh08';
        $app = Factory::officialAccount($config);
        foreach ($days as $day) {
            $users = User::query()->where('is_member',1)
                ->whereNotNull('open_id')
                ->whereDate('expire_time',Carbon::now()->addDays($day)->format('Y-m-d'))->get();
            if($users->count()>0){
                foreach ($users as $user) {
                    $res = $app->template_message->send([
                        'touser' => $user->open_id,
                        'template_id' => $templateId,
                        'url' => 'https://ctbsc-wap.lyxinxiangyi.cn/vip',
                        'data' => [
                            'thing1' => $user->nick_name??'池塘边素材会员',
                            'time3' => $user->expire_time,
                            'thing5' => '池塘边素材',
                            'thing6' => "您的会员还有 $day 天过期，请及时充值"
                        ]
                    ]);
                    if($res['errmsg']!='ok'){
                        dump($user->id,$user->open_id,$res);
                    }
                }
            }

        }
        return 0;
    }
}
